/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Initialising.java
 *
 * Created on Oct 29, 2012, 5:10:08 PM
 */
package camera;

import UserInterface.FinalGUI;
import java.awt.event.ActionEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import com.googlecode.javacv.cpp.opencv_calib3d.*;
import static com.googlecode.javacv.cpp.opencv_core.*;
import com.googlecode.javacv.cpp.opencv_calib3d.*;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.Timer;
import sun.org.mozilla.javascript.internal.regexp.SubString;
import utilities.CoordinateFinder;
import utilities.TouchPad;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;

/**
 *
 * @author Home
 */
public class Initialising extends javax.swing.JFrame {

    /** Creates new form Initialising */
    public Initialising() {
        initComponents();
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    int[] im = new int[4];
    int numCam = 0;
    public static IplImage[] imfromcam = new IplImage[4];
    Timer tt1;
    boolean doneAdding = false;
    IplImage tempIm = cvCreateImage(new CvSize(257, 185), 8, 3);
    IplImage tempIm1 = cvCreateImage(new CvSize(257, 185), 8, 3);
    IplImage tempIm2 = cvCreateImage(new CvSize(257, 185), 8, 3);
    IplImage tempIm3 = cvCreateImage(new CvSize(257, 185), 8, 3);
    Discovering[] ds = new Discovering[4];
    Thread[] th = new Thread[4];

    public static void setimfromcam(IplImage img, int number) {
        imfromcam[number] = img;
    }
    AbstractAction explcam = new AbstractAction() {

        public void actionPerformed(ActionEvent e) {


            ImageIcon[] ic1 = new ImageIcon[4];
            for (int i = 0; i < imfromcam.length; i++) {
                ic1[i] = new ImageIcon();
                if (im[i] != 0) {
                    try {
                        if (i == 0) {
                            cvResize(imfromcam[i], tempIm);
                            ic1[i].setImage(tempIm.getBufferedImage());
                        }
                        if (i == 1) {
                            cvResize(imfromcam[i], tempIm1);
                            ic1[i].setImage(tempIm1.getBufferedImage());
                        }
                        if (i == 2) {
                            cvResize(imfromcam[i], tempIm2);
                            ic1[i].setImage(tempIm2.getBufferedImage());
                        }
                        if (i == 3) {
                            cvResize(imfromcam[i], tempIm3);
                            ic1[i].setImage(tempIm3.getBufferedImage());
                        }
                    } catch (NullPointerException ex) {
                    }
                }
            }
            if (im[0] != 0) {
                c0.setIcon(ic1[0]);
            }

            if (im[1] != 0) {
                c1.setIcon(ic1[1]);
            }

            if (im[2] != 0) {
                c2.setIcon(ic1[2]);
            }

            if (im[3] != 0) {
                c3.setIcon(ic1[3]);
            }
            jPanel2.repaint();
        }
    };

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        jPanel1 = new javax.swing.JPanel();
        findCameraB = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        c0 = new javax.swing.JLabel();
        c3 = new javax.swing.JLabel();
        c2 = new javax.swing.JLabel();
        c1 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        lCamCombo = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        setButton = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        rCamCombo = new javax.swing.JComboBox();
        animation = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, jPanel1, org.jdesktop.beansbinding.ELProperty.create("${Cameras}"), this, org.jdesktop.beansbinding.BeanProperty.create("title"));
        bindingGroup.addBinding(binding);

        findCameraB.setFont(new java.awt.Font("Tahoma", 1, 12));
        findCameraB.setText("Find Cameras");
        findCameraB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findCameraBActionPerformed(evt);
            }
        });

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        c0.setFont(new java.awt.Font("Tahoma", 1, 12));
        c0.setBorder(javax.swing.BorderFactory.createTitledBorder("Camera 0"));

        c3.setFont(new java.awt.Font("Tahoma", 1, 12));
        c3.setBorder(javax.swing.BorderFactory.createTitledBorder("Camera 3"));

        c2.setFont(new java.awt.Font("Tahoma", 1, 12));
        c2.setBorder(javax.swing.BorderFactory.createTitledBorder("Camera 2"));

        c1.setFont(new java.awt.Font("Tahoma", 1, 12));
        c1.setBorder(javax.swing.BorderFactory.createTitledBorder("Camera1"));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(c2, javax.swing.GroupLayout.DEFAULT_SIZE, 258, Short.MAX_VALUE)
                    .addComponent(c0, javax.swing.GroupLayout.PREFERRED_SIZE, 258, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(c3, javax.swing.GroupLayout.DEFAULT_SIZE, 272, Short.MAX_VALUE)
                    .addComponent(c1, javax.swing.GroupLayout.DEFAULT_SIZE, 272, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(c1, javax.swing.GroupLayout.DEFAULT_SIZE, 235, Short.MAX_VALUE)
                    .addComponent(c0, javax.swing.GroupLayout.PREFERRED_SIZE, 231, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(c3, javax.swing.GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE)
                    .addComponent(c2, javax.swing.GroupLayout.DEFAULT_SIZE, 217, Short.MAX_VALUE))
                .addContainerGap())
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        lCamCombo.setFont(new java.awt.Font("Tahoma", 1, 12));
        lCamCombo.setMaximumRowCount(4);
        lCamCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                lCamComboActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel1.setText("Select Right Camera");

        setButton.setFont(new java.awt.Font("Tahoma", 1, 12));
        setButton.setText("Set");
        setButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                setButtonActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel2.setText("Select Left  Camera");

        rCamCombo.setFont(new java.awt.Font("Tahoma", 1, 12));
        rCamCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rCamComboActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(setButton)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addGap(36, 36, 36)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(rCamCombo, 0, 94, Short.MAX_VALUE)
                            .addComponent(lCamCombo, 0, 94, Short.MAX_VALUE))))
                .addGap(21, 21, 21))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(rCamCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lCamCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(setButton)
                .addGap(14, 14, 14))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(animation, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 618, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(findCameraB, javax.swing.GroupLayout.PREFERRED_SIZE, 263, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                    .addContainerGap(362, Short.MAX_VALUE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(178, 178, 178)))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(findCameraB)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(38, 38, 38)
                .addComponent(animation, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                    .addContainerGap(491, Short.MAX_VALUE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(185, 185, 185)))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 590, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 652, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void findCameraBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_findCameraBActionPerformed
findCameraB.setEnabled(false);
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension dim = toolkit.getScreenSize();
        CoordinateFinder.setLENGTH(dim.width);

        for (int i = 0; i < 4; i++) {
            ds[i] = new Discovering(i);
            th[i] = new Thread(ds[i]);
            th[i].start();
        }
        try {
            Thread.sleep(3000);
        } catch (InterruptedException ex) {
            Logger.getLogger(Initialising.class.getName()).log(Level.SEVERE, null, ex);
        }
        for (int i = 0; i < imfromcam.length; i++) {
            if (imfromcam[i] != null) {
                im[i] = 1;
                numCam++;

            } else {
                im[i] = 0;
            }
        }

        if (im[0] != 0) {
            rCamCombo.addItem("Camera 0");
            lCamCombo.addItem("Camera 0");
        }

        if (im[1] != 0) {
            rCamCombo.addItem("Camera 1");
            lCamCombo.addItem("Camera 1");
        }

        if (im[2] != 0) {
            rCamCombo.addItem("Camera 2");
            lCamCombo.addItem("Camera 2");
        }

        if (im[3] != 0) {
            rCamCombo.addItem("Camera 3");
            lCamCombo.addItem("Camera 3");
        }

//        if (numCam < 2) {
//            JOptionPane.showMessageDialog(new JInternalFrame(), "Please attach the device first", "ERROR", JOptionPane.ERROR_MESSAGE);
//        }else{
//            doneAdding = true;
//        }


        doneAdding = true;
        tt1 = new Timer(15, explcam);
        tt1.start();

    }//GEN-LAST:event_findCameraBActionPerformed

    private void setButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_setButtonActionPerformed

        String rcamno = (String) rCamCombo.getSelectedItem();
        String lcamno = (String) lCamCombo.getSelectedItem();

try{
        int r = Integer.parseInt(rcamno.substring(7));
        int l = Integer.parseInt(lcamno.substring(7));

        if (numCam < 2) {
            JOptionPane.showMessageDialog(new JInternalFrame(), "Please attach the device first", "Information", JOptionPane.INFORMATION_MESSAGE);
        } else if (r == l) {
            JOptionPane.showMessageDialog(new JInternalFrame(), "Same camera has been selected for left and right", "Error", JOptionPane.ERROR_MESSAGE);
        } else {
            int i = JOptionPane.showConfirmDialog(null, "Cameras will be set.Do you want to continue?", "Confirm", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
            if (i == JOptionPane.YES_OPTION) {

                FinalGUI.setLeft_Camera(l);
                FinalGUI.setRight_Camera(r);

                for (int j = 0; j < 4; j++) {
                    th[j].stop();
                }

//                FinalGUI fg = new FinalGUI();
//                fg.setVisible(true);
                TouchPad tp=new TouchPad(50, 50);
                new Thread(tp).start();

            }
        }}catch (NullPointerException er){
            JOptionPane.showMessageDialog(new JInternalFrame(), "Please find the cameras first","Information",JOptionPane.INFORMATION_MESSAGE);
        }

    }//GEN-LAST:event_setButtonActionPerformed

    private void rCamComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rCamComboActionPerformed
        if (doneAdding) {
            int rc = rCamCombo.getSelectedIndex();
            lCamCombo.removeItemAt(rc);
        }
    }//GEN-LAST:event_rCamComboActionPerformed

    private void lCamComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_lCamComboActionPerformed
//        if (doneAdding) {
//            int rc = lCamCombo.getSelectedIndex();
//            rCamCombo.removeItemAt(rc);
//        }
    }//GEN-LAST:event_lCamComboActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Initialising().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel animation;
    private javax.swing.JLabel c0;
    private javax.swing.JLabel c1;
    private javax.swing.JLabel c2;
    private javax.swing.JLabel c3;
    private javax.swing.JButton findCameraB;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JComboBox lCamCombo;
    private javax.swing.JComboBox rCamCombo;
    private javax.swing.JButton setButton;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
